package dao.impl;

import bean.Account;
import dao.AccountDao;
import util.JDBCUtil;

import java.sql.*;

public class AccountDaoImpl implements AccountDao {
    @Override
    public Account selectByIdAndPass(String accNo, String accPass) {
        Account account = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConn();

            String sql = "select * from ACCOUNT where ACCNO = ? and ACCPASS = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, accNo);
            ps.setString(2, accPass);

            rs = ps.executeQuery();
            if (rs.next()) {
                account = new Account();
                account.setAccNo(accNo);
                account.setAccPass(accPass);
                account.setSex(rs.getString("sex"));
                account.setRealName(rs.getString("realname"));
                account.setMoney(rs.getFloat("money"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, ps, conn);
        }
        return account;
    }

    @Override
    public int update(String accNo, float money) {
        Connection conn = null;
        PreparedStatement ps = null;
        int num = 0;

        try {
            conn = JDBCUtil.getConn();
            String sql = "update ACCOUNT set MONEY = MONEY + ? where ACCNO = ?";
            ps = conn.prepareStatement(sql);

            ps.setFloat(1, money);
            ps.setString(2, accNo);
            num = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(ps, conn);
        }
        return num;
    }

    @Override
    public float selectBalance(String accNo) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        float balance = 0.0f;
        try {
            conn = JDBCUtil.getConn();
            String sql = "select MONEY from ACCOUNT where ACCNO = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, accNo);
            rs = ps.executeQuery();

            if (rs.next()) {
                balance = rs.getFloat("money");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(ps, conn, rs);
        }

        return balance;
    }

    @Override
    public int transferAcc(String fromNo, String toNo, float money) {
        Connection conn = null;
        CallableStatement cs = null;
        int num = 0;

        try {
            conn = JDBCUtil.getConn();
            cs = conn.prepareCall("{call PR_TRANSFER(?,?,?,?)}");

            cs.registerOutParameter(4, Types.INTEGER);
            cs.setString(1, fromNo);
            cs.setString(2, toNo);
            cs.setFloat(3, money);

            cs.execute();
            num = cs.getInt(4);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(cs, conn);
        }

        return num;
    }
}
